import logging

import requests
import pytest

from api import log
from api.api_register_login import ApiRegisterLogin
from api.api_tender import ApiTender
from tool import html_parser, common_assert, read_json_data


class TestTender:
    def setup_method(self, method):
        self.session = requests.Session()
        self.api = ApiTender(self.session)
        ApiRegisterLogin(self.session).api_login()

    def teardown_method(self, method):
        self.session.close()

    @pytest.mark.parametrize("amount,text",read_json_data("tender.json", "tender"))
    def test01_tender(self, amount, text):
        try:
            result = self.api.api_tender(amount)
            log.info("后台投资接口结果为:{}".format(result.json()))
            if text == "OK":
                # 三方充值
                data = html_parser(result)
                result = self.session.post(url=data[0], data=data[1])
                log.info("三方投资接口执行结果为:{}".format(result.text))
                print("三方投资结果为：", result.text)
            common_assert(result, text=text)
        except Exception as e:
            log.error(e)
